package com.example.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送工具类
 * @author 朽
 */
@Slf4j
public class SmsUtils {
    /**
     * 发送短信
     * @param phoneNumbers 手机号
     * @param param 参数
     */
    public static void sendMessage(String phoneNumbers,String param){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tMvfQKKanFE1y73Tk4z", "JFJeIBQ5zCtmCBwSYjAe2PknLEwwiD");
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName("ISHOP");
        request.setTemplateCode("SMS_467160079");

        Map<String, String> templateParam = new HashMap<>();
        templateParam.put("code", param);
        String jsonParam = JSONObject.toJSONString(templateParam);
        request.setTemplateParam(jsonParam);

        try {
            SendSmsResponse response = client.getAcsResponse(request);
        }catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
